package com.example.capsule_chat0322.request;

import android.os.Handler;

import com.example.capsule_chat0322.bean.PositionBean;
import com.example.capsule_chat0322.configuration.Constant;
import com.example.capsule_chat0322.configuration.HttpConstant;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import java.io.IOException;
import java.util.List;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

/**
 * @Author zhuzhiming
 * @Date 2022/5/18 13:42
 */
public class PositionRequest {

    public static void uploadPosition(PositionBean positionBean, Handler handler) {

        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                , new Gson().toJson(positionBean));

        OkHttpClient httpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .get()
                .post(requestBody)
                .url(HttpConstant.HTTP_PREFIX + "/uploadPosition")
                .build();

        httpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                if (response.code() == 200 && response.body().string().equals(Constant.SUCCESS) && handler != null) {
                    handler.sendMessage(handler.obtainMessage(1, Constant.SUCCESS));
                }

            }
        });


    }


    public static void getNearPeople(PositionBean positionBean, Handler handler) {

        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                , new Gson().toJson(positionBean));

        OkHttpClient httpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .get()
                .post(requestBody)
                .url(HttpConstant.HTTP_PREFIX + "/getNearPeople")
                .build();

        httpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                String resBody = response.body().string();

                List<PositionBean> positionBeanList = new Gson().fromJson(resBody, new TypeToken<List<PositionBean>>() {
                }.getType());

                if (positionBeanList != null) {
                    handler.sendMessage(handler.obtainMessage(1, positionBeanList));
                }

            }
        });


    }

    public static void getDistance(PositionBean positionBean, Handler handler) {

        RequestBody requestBody = FormBody.create(MediaType.parse("application/json; charset=utf-8")
                , new Gson().toJson(positionBean));

        OkHttpClient httpClient = new OkHttpClient();
        Request request = new Request.Builder()
                .get()
                .post(requestBody)
                .url(HttpConstant.HTTP_PREFIX + "/getDistance")
                .build();

        httpClient.newCall(request).enqueue(new Callback() {
            @Override
            public void onFailure(Call call, IOException e) {

            }

            @Override
            public void onResponse(Call call, Response response) throws IOException {

                if (response.code() != 200) {
                    return;
                }
                String resBody = response.body().string();

             PositionBean positionBean = new Gson().fromJson(resBody, PositionBean.class);

                if (positionBean != null) {
                    handler.sendMessage(handler.obtainMessage(1, positionBean));
                }

            }
        });


    }
}
